import 'dart:io';

import 'package:flutter/material.dart';
import 'package:lx_widgets_library/lx_widgets_library.dart';
import 'package:video_player/video_player.dart' as vp;

/// 自定义VideoPlayer
/// 支持file类型文件
/// 只是缩略图显示
///
class CustomVideoPlayer extends StatefulWidget {
  const CustomVideoPlayer({
    super.key,
    required this.url,
    this.filePath,
    this.navTitle,
    this.aspectRatio,
  });

  /// Link of the video
  final String? url;

  final String? filePath;

  /// The Aspect Ratio of the Video. Important to get the correct size of the video
  final double? aspectRatio;

  //
  final String? navTitle;

  @override
  VideoPlayerState createState() => VideoPlayerState();
}

class VideoPlayerState extends State<CustomVideoPlayer> {
  late vp.VideoPlayerController _controller;

  @override
  void initState() {
    super.initState();

    Loading.showLoading(null);
    if (widget.url != null) {
      // ignore: deprecated_member_use
      _controller = vp.VideoPlayerController.network(widget.url!)
        ..initialize().then((_) {
          Loading.dismiss();
          setState(() {});
        });
    } else if (widget.filePath != null) {
      _controller = vp.VideoPlayerController.file(File(widget.filePath!))
        ..initialize().then((_) {
          Loading.dismiss();
          setState(() {});
        });
    }
  }

  @override
  void dispose() {
    super.dispose();
    _controller.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.black,
      child: Stack(
        children: [
          Padding(
            padding: EdgeInsets.only(top: statusBarHeight),
            child: Row(
              children: [
                CommonWidgets.appBarLeading(
                  onTap: () {
                    Navigator.of(context).pop();
                  },
                ),
                const SizedBox(width: 10),
                LxText(
                  widget.navTitle,
                  color: Colors.white,
                  fontWeightEnum: LxFontWeightEnum.titleLarge,
                )
              ],
            ),
          ),
          Visibility(
            visible: _controller.value.isInitialized,
            child: Center(
              child: Column(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  Stack(
                    alignment: _controller.value.isPlaying
                        ? Alignment.bottomLeft
                        : Alignment.center,
                    children: [
                      ConstrainedBox(
                        constraints: BoxConstraints(
                          maxWidth: screenWidth, // 最大宽度
                          maxHeight: screenHeight - appBarHeight * 2, // 最大高度
                        ),
                        child: AspectRatio(
                          aspectRatio: widget.aspectRatio ??
                              _controller.value.aspectRatio,
                          child: vp.VideoPlayer(_controller),
                        ),
                      ),
                      IconButton(
                        iconSize: _controller.value.isPlaying ? 24 : 50,
                        onPressed: () {
                          setState(() {
                            debugPrint(
                                'setState -- ${_controller.value.isPlaying}');
                            _controller.value.isPlaying
                                ? _controller.pause()
                                : _controller.play();
                          });
                        },
                        icon: ClipRRect(
                          borderRadius: BorderRadius.circular(25),
                          child: Container(
                            color: Colors.black54,
                            child: Icon(
                              _controller.value.isPlaying
                                  ? Icons.pause
                                  : Icons.play_arrow,
                              color: Colors.white,
                              // size: 60,
                            ),
                          ),
                        ),
                      ),
                    ],
                  ),
                  vp.VideoProgressIndicator(
                    _controller,
                    colors: const vp.VideoProgressColors(
                        playedColor: Colors.white38,
                        bufferedColor: Colors.white12,
                        backgroundColor: Colors.white10),
                    allowScrubbing: true,
                  ),
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }
}
